package com.info.utils;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.omg.CORBA.StringHolder;

public class D_AOC {

	private static Logger logger = Logger.getLogger(D_AOC.class);
	private static List<LALO> D_LAO = null;
	private static double[] s_D_LA;
	private static double[] s_D_LO;
	private static List<LALO> S_LAO = null;

	public static double[] getS_D_LA() {
		return s_D_LA;
	}

	public static void setS_D_LA(double[] s_D_LA) {
		D_AOC.s_D_LA = s_D_LA;
	}

	public static double[] getS_D_LO() {
		return s_D_LO;
	}

	public static void setS_D_LO(double[] s_D_LO) {
		D_AOC.s_D_LO = s_D_LO;
	}

	public static void S_FU() {
		URL url = D_AOC.class.getResource("");
		Document doc = null;
		try {
			SAXReader reader = new SAXReader();
			doc = reader.read(new File(url.getPath() + "SXML.xml"));
			Element root = doc.getRootElement();
			Iterator<Element> itor = root.elementIterator("data");
			Element datainfo = null;
			LALO lalo = null;
			S_LAO=new ArrayList<LALO>();
			while (itor.hasNext()) {
				lalo = new LALO();
				datainfo = itor.next();
				lalo.setSxla(Double.parseDouble(datainfo.elementTextTrim("xla")));
				lalo.setSxlo(Double.parseDouble(datainfo.elementTextTrim("xlo")));
				lalo.setSdla(Double.parseDouble(datainfo.elementTextTrim("dla")));
				lalo.setSdlo(Double.parseDouble(datainfo.elementTextTrim("dlo")));
				lalo.setLa(Double.parseDouble(datainfo.elementTextTrim("la")));
				lalo.setLo(Double.parseDouble(datainfo.elementTextTrim("lo")));
				S_LAO.add(lalo);
			}
			doc = reader.read(new File(url.getPath() + "DXML.xml"));
			Element roots = doc.getRootElement();
			Iterator<Element> itors = root.elementIterator("data");
			Element datainfos = null;
			LALO lalos = null;
			D_LAO=new ArrayList<LALO>();
			while (itor.hasNext()) {
				lalos = new LALO();
				datainfos = itor.next();
				lalos.setSxla(Double.parseDouble(datainfos
						.elementTextTrim("xla")));
				lalos.setSxlo(Double.parseDouble(datainfos
						.elementTextTrim("xlo")));
				lalos.setSdla(Double.parseDouble(datainfos
						.elementTextTrim("dla")));
				lalos.setSdlo(Double.parseDouble(datainfos
						.elementTextTrim("dlo")));
				lalos.setLa(Double.parseDouble(datainfos.elementTextTrim("la")));
				lalos.setLo(Double.parseDouble(datainfos.elementTextTrim("lo")));
				D_LAO.add(lalos);
			}
		} catch (Exception e) {
			logger.info("SHW#解析Google纠偏XML :" + e.getMessage());
		}
	}

	public static void Tranform(String las,String los,StringHolder lah, StringHolder loh) {
		double la=Double.parseDouble(las);
		double lo=Double.parseDouble(los);
		if (S_LAO == null || D_LAO == null) {
			S_FU();
		}
		for (LALO lalo : S_LAO) {
			if (((la < lalo.getSdla()) && (la >= lalo.getSxla()))
					&& ((lo < lalo.getSdlo()) && (lo >= lalo.getSxlo()))) {
				la += lalo.getLa();
				lo += lalo.getLo();
				lah.value=la+"";
				loh.value=lo+"";
				System.out.println(lah.value+"  "+loh.value);
				return;
			}
		}
		for (LALO lalo2 : D_LAO) {
			if (((la < lalo2.getSdla()) && (la >= lalo2.getSxla()))
					&& ((lo < lalo2.getSdlo()) && (lo >= lalo2.getSxlo()))) {
				la += lalo2.getLa();
				lo += lalo2.getLo();
				lah.value=la+"";
				loh.value=lo+"";
				System.out.println(lah.value+"  "+loh.value);
				return;
			}
		}

	}

	public static void main(String args[]) throws Exception {
		StringHolder lah=new StringHolder();
		StringHolder loh=new StringHolder();
		String la="34.24585";
		String lo="108.8959166666666666";
		D_AOC.Tranform(la, lo,lah,loh);
		System.out.println(lah.value+"   "+loh.value);
	}
}
